﻿package app.view.components.guiDima.sky 
{
	import app.model.types.LangTypes;
	import app.view.components.buttons.LangBtn;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import gs.TweenLite;
	
	/**
	 * Облако с вводом текста
	 * @author dmitryi koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class InputSkyDBase extends Sprite
	{
		// app.view.components.guiDima.sky.InputSkyDBase
		
		public var txt:TextField;
		public var counter:MovieClip;
		public var title:MovieClip;
		private var _currentValue:String='';
		private var _mask:Shape;
		private var _len:Number;
		private static const GAP:Number = 150;
		public static const MAX_CHAIR:int = 336;
		private static const DEFAULT_STR:String= '';
		
		private var _positionCursor:int=0;
		private var _currentPositionCursor:int=0;
		
		public var stamp:MovieClip;
		
		// marka
		// shtamp
		// stamp
		
		private var _tweenMask:TweenLite;
		private var _test:TextField;
		
		public function InputSkyDBase() 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			
			
		}
		
		private function addedStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedStage);
			
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			//txt.background = true;
			//txt.backgroundColor = 0xff0000;
			//txt.wordWrap = true;
			//txt.multiline = true;
			//txt.maxChars = 500;
			//txt.embedFonts = true;
			//txt.type = TextFieldType.INPUT;
			//txt.mouseEnabled = false;
			//txt.addEventListener(TextEvent.TEXT_INPUT, onTextInput);
			
			//txt.text= 'asd;lfj a;slkdfj'
			
			hideMarkaStamp();
			
			/*_mask = new Shape();
			_mask.graphics.beginFill(0xff0000,0.3);
			_mask.graphics.drawEllipse(-(this.width+GAP)/2, -(this.height+GAP)/2, this.width+GAP, this.height+GAP);
			_mask.graphics.endFill();
			addChild(_mask);*/
			
			counter.len.text = MAX_CHAIR;
			
			
			//txt.text = DEFAULT_STR;
			
			
			
			
			var form:TextFormat = new TextFormat();
			form.font = '_sans';
			form.align = TextFormatAlign.JUSTIFY;
			form.size = 25;
			form.leftMargin = 10;
			form.rightMargin = 10;
			
			
			_test = new TextField();
			_test.width = txt.width;
			_test.textColor = 0x333333;
			_test.height = txt.height;
			_test.wordWrap = true;
			_test.multiline = true;
			_test.maxChars = 500;
			_test.defaultTextFormat = form;
			_test.type = TextFieldType.INPUT;
			_test.mouseEnabled = false;
			addChild(_test);
			
			_test.x = Math.round(txt.x);
			_test.y = Math.round(txt.y);
			
			
			stage.focus = _test;
			
			positionCursor = 0;
		}
		
		private function onTextInput(e:TextEvent):void 
		{
			//e.preventDefault();
		}
		
		public function setLang(lang:String=LangTypes.RUSSIAN):void
		{
			////trace('444');
			counter.setLang(lang);
			title.setLang(lang);
		}
		
		public function showMark():void
		{
			TweenLite.to(stamp.marka, 0.1, { alpha: 1 ,ease:'none'} );
		}
		
		public function showStamp():void
		{
			TweenLite.to(stamp.shtamp, 0.1, { alpha: 1 ,ease:'none'} );
		}
		
		public function hideMarkaStamp():void
		{
			stamp.marka.alpha = 0;
			stamp.shtamp.alpha = 0;
			
		}
		
		public function hide(time:Number=2):void
		{
			TweenLite.to(this, time, { scaleX: 0,scaleY: 0 , ease:'none' } );
			
		}
		
		public function show(time:Number=2):void
		{
			TweenLite.to(this, 2, { scaleX: 1,scaleY: 1 , ease:'none' } );
		}
		
		public function destroy():void
		{
			_tweenMask ? _tweenMask.clear() : 0;
			
			try 
			{
				removeChild(_test);
			}catch (err:Error)
			{
				
			}
			//removeChild(_mask);
		}
		
		public function get currentValue():String { return _currentValue; }
		
		public function set currentValue(value:String):void 
		{
			_currentValue = value;
			
			//trace('99900',txt,_test);
			
			if (_test) 
			{
				if (_currentValue=='') 
				{
					_test.text = DEFAULT_STR;
					counter.len.text = MAX_CHAIR;
				}else
				{
					//trace('111',_test.text,DEFAULT_STR,_test.text==DEFAULT_STR);
					if (_test.text==DEFAULT_STR) 
					{
						_test.text = _currentValue;
					}else
					{
						//trace('0000');
						_test.text = '';
						_test.text = _currentValue;
						
						if (_test.text.length!=0) 
						{
							counter.len.text = String(MAX_CHAIR - _test.text.length);
							
						}else 
						{
							counter.len.text = String(MAX_CHAIR);
						}
					}
				}
			}
			
			
			/*_test.text = '555555';
			
			if (_test) 
			{
				_test.text = _currentValue;
			}*/
			
			
			//positionCursor = txt.text.length;
			
		}
		
		public function get len():Number { return _len; }
		
		public function set len(value:Number):void 
		{
			_len = value;
		}
		
		public function get positionCursor():int { return _positionCursor; }
		
		public function set positionCursor(value:int):void 
		{
			_positionCursor = value;
			
			//trace('posit',_currentPositionCursor);
			if (stage && _test) 
			{
				stage.focus = _test;
				
				_test.setSelection(_positionCursor, _positionCursor);
				
				currentPositionCursor = _test.caretIndex;
			}
			
		}
		
		public function get currentPositionCursor():int { return _currentPositionCursor; }
		
		public function set currentPositionCursor(value:int):void 
		{
			_currentPositionCursor = value;
		}
		
	}

}